package com.service.testclasses;

import com.container.app.annotations.Inject;
import com.container.app.annotations.ServiceComponent;
import com.kikidao.app.exceptions.DAOCheckedExeption;
import com.kikidao.app.testclasses.useraccounts.User;
import com.kikidao.app.testclasses.useraccounts.UserDAO;
import com.service.annotation.Transactional;
import com.service.exceptions.BusinessServiceCheckedException;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/30/12
 * Time: 2:54 PM
 */

@SuppressWarnings("UnusedDeclaration")
@ServiceComponent
public class RegisterBSImpl implements RegisterBS {

    UserDAO userDAO;

    @Inject
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }


    public String signUp(User newUser) throws BusinessServiceCheckedException {
        try {

            userDAO.create(newUser);

            return "Signing up user " + newUser.getName() + "...";

        } catch (DAOCheckedExeption daoCheckedExeption) {
            throw new BusinessServiceCheckedException("Can't sign up new user " + newUser.getName() + ".", daoCheckedExeption);

        }

    }

    public String logIn(User loginUser) throws BusinessServiceCheckedException {

        try {

            User expected = userDAO.read(loginUser);

            if (!expected.equals(loginUser))
                throw new BusinessServiceCheckedException("Wrong username or password.");
            return "Logging in as " + loginUser.getName() + "...";

        } catch (DAOCheckedExeption daoCheckedExeption) {
            throw new BusinessServiceCheckedException("Can't log because user " + loginUser.getName() + " doesn't exist. ", daoCheckedExeption);
        }

    }


    public User getUserInfo(Integer id) throws BusinessServiceCheckedException {
        try {

            return userDAO.read(new User(null, id, null, null, 10));

        } catch (DAOCheckedExeption daoCheckedExeption) {
            throw new BusinessServiceCheckedException("There is no user with id = " + id, daoCheckedExeption);
        }
    }

    public List<User> listUsers() throws BusinessServiceCheckedException {

        try {

            return userDAO.list();

        } catch (DAOCheckedExeption daoCheckedExeption) {
            throw new BusinessServiceCheckedException("Can't list users. ", daoCheckedExeption);
        }
    }

    public void incrementAllAges() throws BusinessServiceCheckedException {

        List<User> users;
        try {

            users = userDAO.list();

        } catch (DAOCheckedExeption daoCheckedExeption) {

            throw new BusinessServiceCheckedException("Can't list users.", daoCheckedExeption);
        }
        for (User user : users) {

            user.setAge(user.getAge() + 1);
            try {
                userDAO.update(user);
                System.out.println("Updating user..." + userDAO.read(user));
            } catch (DAOCheckedExeption daoCheckedExeption) {

                throw new BusinessServiceCheckedException("Can't update user " + user.getName() + ".", daoCheckedExeption);
            }

            if (user.getAge() >= 100) {

                throw new BusinessServiceCheckedException("Can't have over 100 year old users.");
            }
        }

    }


    public void clearAllUsers() throws BusinessServiceCheckedException {
        List<User> users;
        try {

            users = userDAO.list();
        } catch (DAOCheckedExeption daoCheckedExeption) {

            throw new BusinessServiceCheckedException("Can't list users.", daoCheckedExeption);
        }
        for (User user : users) {
            try {
                userDAO.delete(user);
            } catch (DAOCheckedExeption daoCheckedExeption) {

                throw new BusinessServiceCheckedException("Can't delete user " + user.getName() + ".", daoCheckedExeption);
            }

        }

    }

    @Transactional
    public void replaceUser(User first, User last) throws BusinessServiceCheckedException {


        try {
            if (first.getId() == last.getId())
                userDAO.update(last);
            else {
                userDAO.delete(first);
                userDAO.create(last);

            }
        } catch (DAOCheckedExeption daoCheckedExeption) {
            throw new BusinessServiceCheckedException("Can't replace user " + first.getName() + " with user " + last.getName() + ".", daoCheckedExeption);
        }

        if (first.getAccountType().getCode() == 15 || first.getAccountType().getType().equals("Admin"))
            throw new BusinessServiceCheckedException("Can't delete user " + first.getName() + "!");
    }
}
